import { Component, OnInit, Injector, ViewChild, Input } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent, FileUpload } from 'primeng/primeng';
import {
    StockZT_RoomServiceProxy,
    ListDtoExtensionsItem,
    Stock_RoomEditDto,
    StockZT_LocationServiceProxy,
    Stock_LocationListDto
} from '@shared/service-proxies/stock-service-proxies';
import { CreateOrEditRoomModalComponent } from './create-or-edit-room-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDaterangepickerConfig } from 'ngx-bootstrap';
import { IBasicRoomInfo } from './basic-room-info';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';

@Component({
    templateUrl: './room-detail.component.html',
    animations: [appModuleAnimation()],
    selector: 'room-detail'
})
export class RoomDetailComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('ExcelFileUpload') excelFileUpload: FileUpload;

    @ViewChild('createOrEditRoomModal')
    createOrEditRoomModal: CreateOrEditRoomModalComponent;
    basicRoomInfo: IBasicRoomInfo = null;
    roomId;

    roomdeitailinfo = new Stock_RoomEditDto();
    roomshow = 1;
    locationshow = 0;
    anyncing = false;
    filterText = '';
    userDataPermissions;
    selectedDepts;
    selectedValue = new Stock_LocationListDto();
    minMode: BsDatepickerViewMode = 'month';

    bsConfig: Partial<BsDaterangepickerConfig>;
    extensions: Array<ListDtoExtensionsItem>;
    constructor(
        injector: Injector,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy,
        private _LocationServiceProxy: StockZT_LocationServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this.userDataPermissions = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });

            this.selectedDepts = result.items;        });
    }

    getRoomDetail(roomid) {
        if (roomid != null && roomid != undefined) {
            this._stockZT_RoomServiceProxy.getRoomById(roomid).subscribe(result => {
                this.roomdeitailinfo = result;
            });
        }
    }
    getLocationData(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);
            return;
        }

        if (this.roomId != null && this.roomId != undefined) {
            this.primengTableHelper.showLoadingIndicator();
            this._LocationServiceProxy
                .getLocatioListExtentionByCode(
                    this.roomId,
                    this.filterText,
                    null,
                    this.primengTableHelper.getMaxResultCount(this.paginator, event),
                    this.primengTableHelper.getSkipCount(this.paginator, event)
                )
                .subscribe(result => {
                    this.extensions = result.items;
                    this.primengTableHelper.totalRecordsCount = result.stock_LocationList.totalCount;
                    this.primengTableHelper.records = result.stock_LocationList.items;
                    this.primengTableHelper.hideLoadingIndicator();
                });
        }
    }

    delete(id: string) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._LocationServiceProxy.deleteLocation(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));

                    this.getLocationData();
                });
            }
        });
    }
}
